package itheima.d09;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("小王", 18, 66));
        students.add(new Student("小张", 17, 99));
        students.add(new Student("小李", 26, 20));
        students.add(new Student("小赵", 16, 77));
        students.add(new Student("小刘", 19, 18));
        students.add(new Student("小李", 26, 20));

//获取成绩及格的所用同学，按照成绩降序输出
        students.stream()
                //中间处理
                .filter(s -> s.getScrous() >= 60)
                .sorted( (s1, s2) -> Double.compare(s2.getScrous(), s1.getScrous()))
                //终止处理
                .forEach(System.out::println);

        //获取年龄在18-25之间的所有同学，按照年龄升序输出
        students.stream()
                .filter(s -> s.getAge() >= 17)
                .filter(s -> s.getAge() <= 25)
                .sorted( (s1, s2) -> s2.getAge() - s1.getAge())
                .forEach(System.out::println);


//获取考试前三名
        //方式一：sortend+limit
        students.stream()
                .sorted( (s1,s2) -> Double.compare(s1.getScrous(), s2.getScrous()))
                .limit(3)
                .forEach(System.out::println);


        System.out.println("================================");

        //方式二：sorted+skip
        students.stream()
                .sorted( (s1,s2) -> Double.compare(s1.getScrous(), s2.getScrous()))
                .skip(3)
                .forEach(System.out::println);



        //找出年纪大于18的学生姓名，出去重复的姓名
        students.stream()
                .filter(s -> s.getAge() >= 18)
                .distinct()//hashCode+equals，进行去除
                .forEach(System.out::println);



        //将成绩及格的同学，包装成老师，按照成绩降序输出
        students.stream()
                .filter(s -> s.getScrous() >= 60)
                .map(s -> new Teacher(s.getName(), s.getAge()))
                .sorted( (t1,t2)->t1.getAge() - t2.getAge() )
                .forEach(System.out::println);



        //过滤出未成年
        //过滤出不及格
        //合起来,合并流之前不能进行终结操作，否则流会关闭
        Stream<Student> stream = students.stream();
        stream
                .filter(s -> s.getAge() < 18);
        Stream<Student> stream1 = students.stream();
        stream1
                .filter(s -> s.getScrous() < 60);
        Stream.concat(stream, stream1)
                .forEach(System.out::println);





    }

}
